<template>
	<view class='main'>
<!-- 	  <view class='tab'>
	    <view @click='tabFun(0)' >
	      <text :class="[tabIndex==0?'active':'']">全部</text>
	    </view>
	    <view @click='tabFun(1)' >
	      <text :class="[tabIndex==1?'active':'']">待确认</text>
	    </view>
	    <view @click='tabFun(2)' >
	      <text :class="[tabIndex==2?'active':'']">待发货</text>
	    </view>
	    <view @click='tabFun(3)' >
	      <text :class="[tabIndex==3?'active':'']">已发货</text>
	    </view>
	    <view @click='tabFun(4)' >
	      <text :class="[tabIndex==4?'active':'']">已完成</text>
	    </view>
	  </view>
	
	  <view class="divider-order"/> -->
	  
	  <view v-if="list.length == 0" class="empty-cart">您还没有退货单</view>
	  
	  <scroll-view v-else scroll-y class="list" style="height:100vh;top:0px;" refresher-enabled="true"
	  refresher-threshold="30" :refresher-triggered="triggered" @refresherrefresh="onScrollRefresh" >
								
	    <block v-for="(item,index) in list" :key="index" >
	      
		  <view class='detail-item'>
			<view class="flex-wrap flex-direction-row">
				<image style="margin-right: 18px;" class="goodsimg" :src='item.image_path || "https://cgt.weikedashi.net/cgt/default.png"'></image>
				<text class='title'>{{item.product_name}}</text>
				<image v-if="item.status == 1" @click.stop="del(item.id)" class="delete" src="../../../static/delete.png"/>
			</view>
			
			<view class='time flex-item' style="margin-left: 94px;">
			退货申请时间:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{item.ctime*1000 | formatDate}}</view>
			
			<view class="flex-wrap flex-direction-row" style="margin-top: 6px;">
				<view style="width: 94px;"></view>
				<view class='time flex-item'>退货小结:</view>
				<view class='time flex-item'>¥{{item.price/100}}</view>
				<view class='time flex-item'>×{{item.qty}}</view>
				<view class='time flex-item'>¥{{item.subtotal/100}}</view>
			</view>
			
			<view v-if="item.status == 4" class='time time-black flex-item' style="margin-left: 94px;">
			签收时间:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{item.signforTime}}</view>
			
			<view v-if="item.status > 3" class="flex-wrap flex-direction-row">
				<view style="width: 94px;"></view>
				<view class='time time-black flex-item'>签收小结:</view>
				<view class='time time-black flex-item'>¥{{item.signforPrice/100}}</view>
				<view class='time time-black flex-item'>×{{item.signforQty}}</view>
				<view class='time time-black flex-item'>¥{{item.signforSubtotal/100}}</view>
			</view>
			
			<view class="flex-wrap flex-direction-row" style="margin-top: 10px">
				<view class="exception" :style="'background-color:'+colorMap[item.status]">{{statusMap[item.status]}}</view>
				<view style="flex:1" ></view>  
				<button :disabled="item.status < 2 || item.status > 2"  @click.stop="confirm(item.id, index, item.status)" 
				:class="['route', 'confirm', item.status == 4 ? 'comment' : '']" 
				hover-class="confirm-hover">发货</button>
			</view>		
		  </view>
	    
	    </block>
	  </scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				triggered: false,
				tabIndex: 0,
				isIphoneX: this.$common.isIphoneX,
				statusMap : {1:"待确认", 2:"待发货", 3:"已发货", 4:"已完成"},
				colorMap : {1:"#ff5f19", 2:"#243771", 3:"#999900", 4:"#0a810a"}
			}
		},
		onLoad() {
			this.requestData(false)
		},
		methods: {
			onScrollRefresh(){
				this.requestData(true)
			},
			
			tabFun(e) {
			    this.tabIndex= e
			    this.requestData(false)
			},
			
			  requestData: function(isRefresh){
			    if(isRefresh) this.triggered = true
			    let that = this
				var cid = uni.getStorageSync("userid")
				if (cid == '') return
			    var map = {
			      status: that.tabIndex == 0 ? null : that.tabIndex,
				  customer_id: cid
			    }
				uni.request({
					url: that.$common.serverAddr +'/sales/order/return/items/list',
					data: map,
					method: 'POST',
					success: function(res) {
						console.log(res)
						if (isRefresh) {
							that.$uniApi.tipMsg('刷新成功')
							that.triggered= false
						}
						that.list = res.data.list
					},
					fail: function() {
						that.$uniApi.tipMsg('获取退货信息失败')
					}
				}) 
			  },
			  
			  confirm: function(id, index, status){
			  	let that = this
			  	uni.showModal({
			  	  title: '您确定发货吗？',
			  	  success (res) {
			  	      if (res.confirm) { 
						uni.request({
						  url: that.$common.serverAddr +'/sales/order/return/items/edit',
						  method: 'POST',
						  data: {id: id, status: 3},
						  success: function(res) {
								that.$uniApi.tipMsg(res.data.message)
								if (res.data.status == 200) that.requestData(false)
						  }
						})
			  		}
			  	  }
			  	})
			  },
			
			  showRoute: function(i, ship, code){},
			 
			  pay: function(id, money){  },
			
			del: function(id){
			  let that = this
			  uni.showModal({
			    title: '确定删除吗？',
			    success (res) {
			        if (res.confirm) {
					  uni.request({
					    url: that.$common.serverAddr +'/sales/order/return/items/del/'+id,
					    method: 'POST',
					    success: function(res) {
							that.$uniApi.tipMsg(res.data.message)
							if (res.data.status == 200) that.requestData(false)
					    }
					  })
			        } 
			      }
			  })
			}
		}
	}
</script>

<style>
@import url("../orderlist/orderlist.css")
</style>
